
********************************************************************************
clear all
/*
cap log close
log using LOG/Data_Processing, replace smcl
*/
********************************************************************************

* Adding inflation:
use DTA/Datos_ENIA_95_07_Raw, clear

* Labels de todo:
do DO/Labels_ENIA.do

rename *, lower

merge m:1 year using SOURCE/Inflacion_2018
drop if _merge == 2
drop _merge
*replace inf = inf/1000 // CHECK: this is to transform monetary values to million pesos.
*format %12.0g inf

********************************************************************************
* Section 1: Total workers and measures by quarter
********************************************************************************
{
* Creating variables directly to avoid some mistakes in the database (average do not coincide with averages of quarterly data). This same definitions/variables aggrupation allows us to create the variables by quarter, so I'll use that to create the quarterly data too. Since the variables per quarter does not come constructed this check up allows to verify that we are including the same variables than they did in the data for the yearly data.'

egen tothom_constructed = rowtotal(admprh comprh dirprh espprh manprh ndiprh ninprh proprh serprh trauxprh adseprh prdiprh)
egen totmuj_constructed = rowtotal(admprm comprm dirprm espprm manprm ndiprm ninprm proprm serprm trauxprm adseprm prdiprm)
gen tottrab_constructed = tothom_constructed + totmuj_constructed
gen tottrab = tothom + totmuj

forvalues q = 1/4 {
egen tothom_constructed_t`q'  = rowtotal(admh`q't comh`q't dirh`q't esph`q't manh`q't ndih`q't ninh`q't proh`q't serh`q't trauxh`q't venh`q't adseh`q't prdih`q't)
egen totmuj_constructed_t`q'  = rowtotal(admm`q't comm`q't dirm`q't espm`q't manm`q't ndim`q't ninm`q't prom`q't serm`q't trauxm`q't venm`q't adsem`q't prdim`q't)
egen tottrab_constructed_t`q' = rowtotal(tothom_constructed_t`q' totmuj_constructed_t`q')
}
gen data_error_hom = tothom != tothom_constructed
gen data_error_muj = totmuj != totmuj_constructed
gen data_error_tot = tothom + totmuj != tottrab_constructed
gen data_error_any_worker = data_error_hom | data_error_muj | data_error_tot
replace data_error_any_worker = 0 if year == 2012 // I checked the cases here and they are all within the range +-2. This is equivalent to 0.5 per quarter. The difference thus comes from rounding in different ways (i.e. before or after the sum over quarters).
drop if data_error_any_worker == 1
* 173 observations dropped (out of 70,000), most of them form year 2000. None of these differences come from years where the variables where created by the INE (and thus double checked). They all occurr in years where respondents filled the total and the detailed values themselves, without double checking. I tried checking many different combinations to see what could be leading the problem but I couldn't come up with a solution. I preferred to drop them because they induce a difference with estimations by quarter. This is because I need to manually create the variables "tothom" and "totmuj" by quater, since they don't come in the data. This allows me to make sure the variables included in the creation of these variables is consistent with yearly totals.

* This could be anywhere but since I'm checking the other variable I'll also check these two that are used later. A minimum amount of numbers change, about 0.1 percent.
replace va = . if va > vbp // va is vbp - costs
replace rempag = . if rempag == 0 // This includes owners so it should be higher than 0.

* For cases when not all quarters were available it makes more sense to me to simply not include them in the data, instead of considering them as 0 employment in that given quarter.
forvalues q = 1/4 {
replace tothom_constructed_t`q'  = . if tottrab_constructed_t`q' == 0
replace totmuj_constructed_t`q'  = . if tottrab_constructed_t`q' == 0
replace tottrab_constructed_t`q' = . if tottrab_constructed_t`q' == 0
label var tothom_constructed_t`q' "Total Men (Q`q')"
label var totmuj_constructed_t`q' "Total Women (Q`q')"
label var tottrab_constructed_t`q' "Total Workers (Q`q')"
}
label var tothom_constructed "Total Men (constructed)"
label var totmuj_constructed "Total Women (constructed)"
label var tottrab_constructed "Total Workers (constructed)"

replace tothom  = . if tottrab == 0
replace totmuj  = . if tottrab == 0
replace tottrab = . if tottrab == 0
label var tothom "Total Men (original variable)"
label var totmuj "Total Women (original variable)"
label var tottrab "Total Workers (original variable)"

egen tothom_mean  = rowmean(tothom_constructed_t*)
egen totmuj_mean  = rowmean(totmuj_constructed_t*)
egen tottrab_mean = rowmean(tottrab_constructed_t*)
label var tothom_mean "Total Men (Quarters Avg.)"
label var totmuj_mean "Total Women (Quarters Avg.)"
label var tottrab_mean "Total Workers (Quarters Avg.)"

* Workers without a contract (subcontracted + home office and comission sellers). This is following the definition in the ENIA:
label var tohsc "Total Men Without Conctract"
label var tomsc "Total Women Without Conctract"
label var tosc "Total Workers Without Conctract"

* Relevant variables created in this section:
desc tothom totmuj tothom_constructed totmuj_constructed tothom_constructed_t* tohsc tomsc data_error_any_worker
}
********************************************************************************
* Section 2: Classify workers types
********************************************************************************
{
* Classify workers according to whether they are high or low skills and according to whether they seem to be automatable or not (i.e. more or less replaceable by machines). The high/low skills classification is intended to measure differences in wage of these types of workers rather than the possibility of automation, so this could capture different effects.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* Section 2.1: Automatable vs Non-automatable
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

egen autom_men       = rowtotal(admprh ndiprh ninprh trauxprh adseprh)
egen non_autom_men   = rowtotal(dirprh espprh manprh proprh serprh comprh prdiprh)
egen autom_women     = rowtotal(admprm ndiprm ninprm trauxprm adseprm)
egen non_autom_women = rowtotal(dirprm espprm manprm proprm serprm comprm prdiprm)

label variable autom_men "Automatable Work - Men"
label variable autom_women "Automatable Work - Women"
label variable non_autom_men "Non-Automatable Work - Men"
label variable non_autom_women "Non-Automatable Work - Women"

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* Section 2.2: High vs Low Skills (High/Medium/Low)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

* The survey main intentions when diviing the categories is not exactly sorting workers skills but their tasks within the factory. To identify the categories I'll then use the definition of the survey questions plus the average wage for each sector. In particular for commision workers and administrative workers, for which the definitions less precise, so I sort them based on their wages relative to other areas.
gen wageadm    = remadmin/(admprh+admprm)   if remadmin != 0
gen wagedirect = remdirect/(dirprh+dirprm)  if remdirect != 0
gen wageesp    = remtesp/(espprh+espprm)    if remtesp != 0
gen wageprop   = remprop/(proprh+proprm)    if remprop != 0
gen wagecom    = remcoms/(comprh+comprm)    if remcoms != 0
gen wageaux    = remaux/(trauxprh+trauxprm) if remaux != 0
gen wageserv   = remserv/(serprh+serprm)    if remserv != 0
gen wagenocali = remnocald/(ndiprh+ndiprm)  if remnocald != 0
sum wagedirect wageprop wagecom wageesp wageadm wageaux wageserv wagenocali if year > 2000
* For this I'm only using year 2000+ because the categories definition were more stable after that year.

* High/Low skilled workers by gender:
egen hs_m = rowtotal(dirprh espprh proprh comprh prdiprh)
egen ls_m = rowtotal(trauxprh serprh ndiprh manprh ninprh admprh adseprh)
egen hs_w = rowtotal(dirprm espprm proprm comprm prdiprm)
egen ls_w = rowtotal(manprm ninprm trauxprm serprm ndiprm admprm adseprm)

label var hs_m "High Skill Men (High vs Low)"
label var hs_w "High Skill Women (High vs Low)"
label var ls_m "Low Skill Men (High vs Low)"
label var ls_w "Low Skill women (High vs Low)"
/*
* XXX: OLDER VERSION FOR REFERENCE
* High/Low skilled workers by gender:
egen hs_m = rowtotal(proprh dirprh espprh admprh)
egen ls_m = rowtotal(comprh ndiprh ninprh serprh)
*/

* High/Medium/Low skilled workers by gender (alternative definition):
egen HS_M = rowtotal(dirprh espprh proprh comprh prdiprh)
egen MS_M = rowtotal(admprh trauxprh adseprh) 
egen LS_M = rowtotal(ndiprh ninprh serprh manprh)
egen HS_W = rowtotal(dirprm espprm proprm comprm prdiprm)
egen MS_W = rowtotal(admprm trauxprm adseprm)
egen LS_W = rowtotal(ndiprm ninprm serprm manprm)
/*
* High/Medium/Low skilled workers by gender (alternative definition):
egen HS_M = rowtotal(dirprh espprh)
egen MS_M = rowtotal(admprh venprh comprh)
egen LS_M = rowtotal(ndiprh manprh trauxprh serprh ninprh)
*/

label var HS_M "High Skill Men (High vs Low)"
label var HS_W "High Skill Women (High vs Low)"
label var MS_M "Medium Skill Men (High vs Med vs Low)"
label var MS_W "Medium Skill Women (High vs Med vs Low)"
label var LS_M "Low Skill Men (High vs Med vs Low)"
label var LS_W "Low Skill women (High vs Med vs Low)"

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* Section 2.3: Wages for High vs Low Skills (High/Medium/Low)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

egen wage_hs = rowtotal(remprop remdirect remtesp remcoms remhprop remhdirect remhcoms remhtesp remhprdi)
egen wage_ls = rowtotal(remnocald remaux remserv remobr remnocali remmant remadmin remhnocald remhaux remhadmin remhserv remhadse)

* Relevant variables created in this section:
desc autom_men autom_women non_autom_men non_autom_women hs_m hs_w ls_m ls_w HS_M HS_W MS_M MS_W LS_M LS_W
}
********************************************************************************
* Section 3: Capital
********************************************************************************
{

* Construct capital variable for missing years
egen k_total      = rowtotal(salter saledi salmaq salveh salmue salotr salret salotri salsof saloat)
gen capital_error = k_total != vstk if year > 2000
replace vstk      = . if year == 2000 // All values are zero.
label var k_total "Total Capital"
* OBS: by doing this instead of using vstk directly (the variable that comes with the ENIA) we can recover the data for all years (while vstk is not defined for years before 2001 and for 2005).

* The differences in vstk and k_total are absolutely minimal so I assume they are rounding errors or something like that. The highest error correponds to 0.0001 percent.
gen diff_percent = (k_total - vstk) / vstk
sum diff_percent
drop capital_error
replace vstk = k_total

* I think checking each category separately may add too much noise so I prefer to keep the definition below as "replaceable" vs "non-replaceable" by workers.
gen k_rep      = salmaq // replaceable by workers
egen k_nrep    = rowtotal(salveh salter saledi salmue salotr salret salotri salsof saloat) // Non-replaceable by workers
replace k_rep  = inf*k_rep
replace k_nrep = inf*k_nrep
label var k_rep "Substitute Capital (Machinery)"
label var k_nrep "Non-substitute (Vehicles + Real Estate)"

* NOTE: I didnt't update this for 2008+ since we are not really using it and they added more categories in that year.
* Capital acquisition:
egen k_acquired = rowtotal(cbnter cbnedi cbnmaq cbnveh cbuter cbuedi cbumaq cbuveh refter refedi refmaq refveh vbnter vbnedi vbnmaq vbnveh vocter vocedi vocmaq vocveh) // Fixed assets purchased new/old, improvement to existing ones and improvements in progress.
egen k_sold = rowtotal(vbuter vbuedi vbumaq vbuveh) // Sold capital assets.
gen k_change_total = k_acquired - k_sold
// OBS: we could also include monetary correction (inflation adjustment) into the calculation -the variable that comes in the database uses it-, but I think that's not the part we want to focus on.
label var k_change_total "Capital Change"
* NOTE: I didnt't update this for 2008+ since we are not really using it and they added more categories in that year.
* Capital acquisition by type:
egen k_rep_acquired    = rowtotal(cbnmaq cbumaq refmaq vbnmaq vocmaq)
egen k_rep_sold        = rowtotal(vbumaq) // Sold capital assets.
gen k_rep_change_total = k_rep_acquired - k_rep_sold
label var k_rep_change "Substitute Capital (Change)"
egen k_nrep_acquired   = rowtotal(cbnter cbnedi cbnveh cbuter cbuedi cbuveh refter refedi refveh vbnter vbnedi vbnveh vocter vocedi vocveh) 
egen k_nrep_sold       = rowtotal(vbuter vbuedi vbuveh)
gen k_nrep_change      = k_nrep_acquired - k_nrep_sold
label var k_rep_change "Non-substitute Capital (Change)"

* Relevant variables created in this section:
desc k_total k_rep k_nrep k_change_total k_rep_change k_nrep_change
}
********************************************************************************
* Section 4: Classification by sectors intensity (K/L, M/W, etc)
********************************************************************************
{
* Main sectors:
gen ciiu_small = round(ciiu3/100)
drop if ciiu_small == . // We use this everywhere so I don't want to keep data that doesn't even have sectors defined.
* We do this categorization by broader areas to avoid individual firms having a a very large weight in some of the more specific sectors.

preserve
tempfile sectors_ratios
* To define the sectors' relative factors intensity I'll use the firms that are not affectd by the policy, since the others could be altered by the extra cost.
gen wm_ratio        = totmuj / tothom
gen kw_ratio        = k_total / totmuj
gen kwage_ratio     = k_total / rempag
gen high_low_skills = (hs_m + hs_w) / (ls_m + ls_w)
gen wagel_ratio     = rempag / tottrab
gen val_ratio       = va / tottrab
gen firm_size       = tottrab

drop if ciiu_small == . | year >= 2008 | (totmuj >= 10 & totmuj <= 30)

collapse wm_ratio kw_ratio kwage_ratio high_low_skills wagel_ratio val_ratio firm_size (sum) tottrab totmuj (count) nui, by(ciiu_small)

* Proportion of workers hired by each sector:
egen total_women = sum(totmuj)
gen prop_women_sector = totmuj/total_women
egen total_workers = sum(tottrab)
gen prop_workers_sector = tottrab/total_workers
egen total_firms = sum(nui)
gen prop_firms_sector = nui/total_firms

* We sort it this way to give "1" to the ones with the highest ratio and "23" to the lowest ratio.
egen wm_order = rank(-wm_ratio)
egen hi_lo_order = rank(-high_low_skills)
egen kw_order = rank(-kw_ratio)
egen kwage_order = rank(-kwage_ratio)
egen wagel_order = rank(-wagel_ratio)
egen val_order  = rank(-val_ratio)
egen size_order = rank(-firm_size)
save `sectors_ratios'
restore

merge n:1 ciiu_small using `sectors_ratios', keepusing(*_order prop_women_sector prop_workers_sector prop_firms_sector)
drop _merge

label var wm_order "W/M (Sector Avg. Intensity)"
label var kw_order "K/L (Sector Avg. Intensity)"
label var kwage_order "K/Wages (Sector Avg. Intensity)"
label var hi_lo_order "High/Low Skills Workers (Sector Avg. Intensity)"
label var wagel_order "Wages/L (Sector Avg. Intensity)"
label var val_order "Y/L (Sector Avg. Intensity)"
label var size_order "Tot. Workers Avg."

corr *_order 

* Relevant variables created in this section:
desc wm_order kw_order kwage_order hi_lo_order wagel_order val_order
}
********************************************************************************
* Section 5: Define subsamples for heterogeneity analysis:
********************************************************************************
{
gen all = 1
gen period = year<=1998
replace period = 2 if year>1998 & year<2003
replace period = 3 if year>=2003 & year<=2007
replace period = 4 if year>=2008  
gen period1 = period == 1
gen period2 = period == 2
gen period3 = period == 3
gen period4 = period == 4

tab wm_order if period <= 3
gen sectorA = wm_order <= 7 if period <= 3 // The 7 first sectors get up to percentile 47.55
gen sectorB = sectorA == 0 if sectorA != .
gen sectorA_old = (ciiu_small==15|ciiu_small==17|ciiu_small==18|ciiu_small==19|ciiu_small==33) if ciiu_small != . & period <= 3
gen sectorB_old = sectorA_old == 0 if sectorA_old != .

gen large = totmuj + tothom > 100 if totmuj + tothom != . & period <= 3
gen small = large == 0 if large != .

tab kw_order if period <= 3
gen sectorC = kw_order <= 10 if period <= 3 // The 10 first sectors get up to percentile 42.56 and then jumps to 69.75
gen sectorD = sectorC == 0 if sectorC != .

global Subsamples all period1 period2 period3 sectorA sectorB small large //sectorC sectorD // period4 sectorA_old sectorB_old
}
********************************************************************************
* Section 6: Regressions outcomes
********************************************************************************
{
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* Secion 6.1: Create outcomes
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

* Main:
gen mw         = tothom/totmuj
gen totmale    = tothom
gen kl         = vstk*inf/(1*(tottrab))
gen k_stock    = vstk*inf

label var mw "Men/Women Workers"
label var k_stock "Capital Stock"
label var kl "Capital per Worker"
label var totmale "Male Workers"

* Values per worker:
gen elecl      = elecons*inf/(1*(tottrab))
gen combl      = tcoval*inf/(1*(tottrab)) // CHECK: changed tcomco to tcoval when adding years 2008+
label var elecl "Electricity per Worker"
label var combl "Fuel per Worker"

* Values per female worker:
gen double kw         = vstk*inf/(1*(totmuj))

gen elecw      = elecons*inf/(1*(totmuj))
gen combw      = tcoval*inf/(1*(totmuj))
gen yw         = va*inf/(1*(totmuj))
gen yw_nolog   = yw

* Values per male worker:
gen double km         = vstk*inf/(1*(tothom))
gen elecm      = elecons*inf/(1*(tothom))
gen combm      = tcoval*inf/(1*(tothom))
gen ym         = va*inf/(1*(tothom))
gen ym_nolog   = ym

label var kw "Capital per Women"
label var elecw "Electricity per Women"
label var combw "Fuel per Women"
label var yw "Product per Women"
label var yw_nolog "Product per Women (no logs)"

label var km "Capital per Men"
label var elecm "Electricity per Men"
label var combm "Fuel per Men"
label var ym "Product per Men"
label var ym_nolog "Product per Men (no logs)"

* Capital types:
gen kl_rep     = k_rep*inf/(1*(tothom+totmuj))
gen kl_nrep    = k_nrep*inf/(1*(tothom+totmuj))
gen kw_rep     = k_rep*inf/(1*(totmuj))
gen kw_nrep    = k_nrep*inf/(1*(totmuj))
gen km_rep     = k_rep*inf/(1*(tothom))
gen km_nrep    = k_nrep*inf/(1*(tothom))

* Changes in capital:
*gen k_change     = k_change_total
gen krep_change  = k_rep_change
gen knrep_change = k_nrep_change

label var kl_rep "Machinery per Worker"
label var kl_nrep "Other Capital per Worker"
label var kw_rep "Machinery per Women"
label var kw_nrep "Other Capital per Women"
label var km_rep "Machinery per Men"
label var km_nrep "Other Capital per Men"

label var k_change_total "Capital Change"
label var krep_change "Machinery Change"
label var knrep_change "Other Capital Change"

* High/Low skills ratio:
gen hl_skills_l   = (hs_m+hs_w)/(ls_m+ls_w)
gen hl_skills_w = hs_w/ls_w
gen hl_skills_m = hs_m/ls_m

* High/Medium/Low skills ratio:
gen hl3_skill_l = (HS_M+HS_W)/(LS_M+LS_W) // high skill
gen ml3_skill_l = (MS_M+MS_W)/(LS_M+LS_W) // medium skill
gen hl3_skill_w = HS_W/LS_W 
gen hl3_skill_m = HS_M/LS_M
gen ml3_skill_w = MS_W/LS_W
gen ml3_skill_m = MS_M/LS_M

label var hl_skills_l "High-skilled/Low-skilled Workers"
label var hl_skills_w "High-skilled/Low-skilled Women"
label var hl_skills_m "High-skilled/Low-skilled Men"

label var hl3_skill_l "High-skilled/Low-skilled Workers (3 types)"
label var ml3_skill_l "Medium-skilled/Low-skilled Workers (3 types)"
label var hl3_skill_w "High-skilled/Low-skilled Women (3 types)"
label var ml3_skill_w "Medium-skilled/Low-skilled Women (3 types)"
label var hl3_skill_m "High-skilled/Low-skilled Men (3 types)"
label var ml3_skill_m "Medium-skilled/Low-skilled Men (3 types)"

* Automatable:
gen autom_ratio_w = autom_women/non_autom_women
gen autom_ratio_m = autom_men/non_autom_men

label var autom_ratio_w "Autom/Non-Autom Women"
label var autom_ratio_m "Autom/Non-Autom Men"

* Wages - High and Low Skilled:
gen wage_ratio = wage_hs/wage_ls

label var wage_ratio "Wage Ratio High/Low Skilled"
label var wage_hs "Wage High Skilled"
label var wage_ls "Wage Low Skilled"

* Other outcomes:
gen totworkers = tottrab
gen wage       = rempag*inf/(tothom+totmuj)
gen yl         = va*inf/tottrab
gen uti_l      = utiret*inf/tottrab
gen uti_w      = utiret*inf/totmuj

gen cost_l = costot*inf/tottrab
gen ing_l = ingtot*inf/tottrab

* Subcontracted workers:
gen msub_w     = tohsc/tothom
gen wsub_w     = tomsc/totmuj
gen mwsub      = tohsc/tomsc

label var totworkers "Total Workers"
label var wage "Wage per Worker"
label var yl "Value Added per Worker"
label var uti_l "Profits per Worker"
label var uti_w "Profits per Women"
label var cost_l "Costs per Worker"
label var ing_l "Revenues per Worker"

label var msub_w "Proportion of Subcontracted Men"
label var wsub_w "Proportion of Subcontracted Women"
label var mwsub "Subcontracted Men per Subcontracted Women"

gen priv_dom = forpro==1 if forpro!=.
gen priv_foreign = forpro==2 | forpro==4 if forpro!=.
egen retiro_div = rowtotal(proret divret)
replace retiro_div = . if proret==. & divret==.

label var priv_dom "Domestic owner"
label var priv_foreign "Foreign owner"
label var diatra "Work days"
label var diapar "Closure days"
label var gexval "Export expenses"
label var fleval "Freight Costs"
label var segval "Insurance primes" // 
label var arrval "Real estate leasing"
label var pubval "Marketing expenses"
label var renimp "Profit taxes"
label var timimp "Registry taxes"
label var patimp "Patents-licenses"
label var conimp "Real state taxes"
label var proret "Owners withdrawal"
label var divret "Dividends"
label var isubval "Subsidies"
label var provap "Items in progress"
label var acavap "Items finished" 
label var acthab "Assets" // 
label var pasdeb "Liabilities" //
label var gashab "Expenses"
label var ingdeb "Income"
label var retiro_div "Dividends"
label var matpco "Raw material purchase"

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* Section 6.2: Outcomes Lists
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

global Main mw kw
global Capital_Usage km elecl combl elecw combw elecm combm
global Capital_Types kw_rep kw_nrep km_rep km_nrep  kl_rep kl_nrep 
global Capital_Change k_change_total krep_change knrep_change 
global HighLow_Skills hl_skills_l hl_skills_w hl_skills_m
global HighMediumLow_Skills hl3_skill_l ml3_skill_l hl3_skill_w hl3_skill_m ml3_skill_w ml3_skill_m 

global Others totworkers wage totmale kl k_stock cost_l ing_l
global Firms_Differences yl yw ym uti_l uti_w 

global Subcontracted msub_w wsub_w mwsub autom_ratio_w autom_ratio_m
global Vars_2000_Plus wage_ratio wage_hs wage_ls

global BalanceVars gexval segval arrval pubval renimp timimp patimp conimp isubval provap acavap matpco diatra diapar priv_dom priv_foreign 
tabstat $BalanceVars, by(year) stats(mean) // These should all be available for all years

global Log_Outcomes "Main" "Capital_Usage" "Capital_Types" "HighLow_Skills" "HighMediumLow_Skills" "Others"
global Non_Log_Outcomes "Capital_Change" "Subcontracted" "Firms_Differences" // mainly because these could be zero/negative
global Extra_Outcomes "Vars_2000_Plus"

global Outcomes_List "Main"  "Capital_Usage"  "BalanceVars" "Capital_Types" "HighLow_Skills" "HighMediumLow_Skills" "Others" "Capital_Change" "Subcontracted" "Firms_Differences"

* Log Outcomes:
local Vars_NoRepeateds
foreach q in $Log_Outcomes $Vars_2000_Plus {
local Var_list ${`q'} 
local Vars_NoRepeateds : list Vars_NoRepeateds | Var_list // This is to make sure we dont apply the logarithm twice in case it appears twice.
}
foreach var of varlist `Vars_NoRepeateds' { // When telling stata that it is a varlist it will automatically delete repetitions
display "`var'"
count if `var' < 0
replace `var' = log(`var')
label var `var' "`: var label `var'' (log)"
}

replace totworkers = log(totworkers)
label var totworkers "Total Workers (log)"
}
********************************************************************************

* Rename vars
rename kw kw_total
rename km km_total
save DTA/Datos_ENIA_95_07_Processed, replace

********************************************************************************

*cap log close
